package com.ruanyun.modular.ry.service.impl;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruanyun.base.pojo.node.ZTreeNode;
import com.ruanyun.base.pojo.page.LayuiPageFactory;
import com.ruanyun.base.pojo.page.LayuiPageInfo;
import com.ruanyun.modular.ry.entity.TDict;
import com.ruanyun.modular.ry.mapper.TDictMapper;
import com.ruanyun.modular.ry.model.params.TDictParam;
import com.ruanyun.modular.ry.model.result.TDictResult;
import com.ruanyun.modular.ry.service.TDictService;
import com.ruanyun.modular.ry.vo.req.TDictReqVo;
import com.ruanyun.modular.ry.vo.resp.TDictRespVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 数据字典表 服务实现类
 * </p>
 *
 * @author 刘耀
 * @since 2020-12-15
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TDictServiceImpl extends ServiceImpl<TDictMapper, TDict> implements TDictService {

    @Override
    public void add(TDictParam param){
        TDict entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(TDictParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(TDictParam param){
        TDict oldEntity = getOldEntity(param);
        TDict newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public TDictResult findBySpec(TDictParam param){
        return null;
    }

    @Override
    public List<TDictResult> findListBySpec(TDictParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(TDictParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 左侧树
     *
     * @return
     */
    @Override
    public List<ZTreeNode> tree() {
        return this.baseMapper.tree();
    }

    /**
     * 分页查询
     *
     * @param tDictReqVo
     * @return
     */
    @Override
    public LayuiPageInfo findPage(TDictReqVo tDictReqVo) {
        Page pageContext = getPageContext();
        IPage<TDictRespVo> page = this.baseMapper.findPage(pageContext, tDictReqVo);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 添加
     *
     * @param tDictParam
     * @return
     */
    @Override
    public ResponseData addItem(TDictParam tDictParam) {
        TDict entity = getEntity(tDictParam);
        Integer resultCount = this.baseMapper.selectCount(Wrappers.<TDict>lambdaQuery().eq(TDict::getCode, entity.getCode()));
        if (resultCount > 0) {
            return ResponseData.error("该编码已存在");
        }
        entity.setCreateTime(new Date());
        int result = this.baseMapper.insert(entity);
        if (result == 0) {
            return ResponseData.error("添加失败");
        }
        return ResponseData.success();
    }


    @Override
    public ResponseData findOne(String code) {
        TDict dict = baseMapper.selectOne(new QueryWrapper<TDict>().lambda().eq(TDict::getCode, code));
        if (dict == null) {
            return ResponseData.error("字典不存在");
        }
        return ResponseData.success(dict);
    }

    @Override
    public ResponseData fineList(String code) {
        TDict dict = baseMapper.selectOne(new QueryWrapper<TDict>().lambda().eq(TDict::getCode, code));
        if (dict == null) {
            return ResponseData.error("字典不存在");
        }
        List<TDict> tDicts = baseMapper.selectList(new QueryWrapper<TDict>().lambda().eq(TDict::getParentId, dict.getId()));
        return ResponseData.success(tDicts);
    }



    private Serializable getKey(TDictParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private TDict getOldEntity(TDictParam param) {
        return this.getById(getKey(param));
    }

    private TDict getEntity(TDictParam param) {
        TDict entity = new TDict();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

     //获取污染物种类
     @Override
     public List<TDictRespVo> getPollutantsTypeList() {
         return baseMapper.getPollutantsTypeList();
     }

}
